<?php

namespace common\logics\order;

use common\helpers\Url;

/**
 * 订单逻辑模型
 * 
 * @author emhome<emhome@163.com>
 * @since 2.0
 */
trait OrderHandleTrait {

    /**
     * 判断当前订单是否可以申请退款
     * @inheritdoc
     */
    public function getCanRefund() {
        if (!$this->isPay) {
            $this->addError('id', '订单还未支付');
            return false;
        }
        if ($this->refund) {
            $this->addError('id', '已提交退款申请');
            return false;
        }
        if ($this->status >= static::STATUS_RECEIVED) {
            $this->addError('id', '当前订单已收货，请提交申请售后退货/退款');
            return false;
        }
        return true;
    }

    /**
     * 判断当前订单是否可以确认收货
     * @inheritdoc
     */
    public function getCanReceive() {
        if (!$this->isPay) {
            $this->addError('id', '订单还未支付');
            return false;
        }
        if ($this->refund) {
            $this->addError('id', '已提交退款申请');
            return false;
        }
        if ($this->status == static::STATUS_SHIPPED) {
            return true;
        }
        return false;
    }

    /**
     * 判断当前订单是否显示查看物流
     * @inheritdoc
     */
    public function getCanLogistics() {
        if (!$this->isPay) {
            $this->addError('id', '订单还未支付');
            return false;
        }
        if ($this->refund) {
            $this->addError('id', '已提交退款申请');
            return false;
        }
        if ($this->status == static::STATUS_SHIP || $this->status == static::STATUS_SHIPPED) {
            return true;
        }
        return false;
    }

    /**
     * 取消按钮
     * @inheritdoc
     */
    public function getBtnCancel() {
        if ($this->canCancel) {
            return [
                'type' => 'default',
                'text' => '取消订单',
                'weblink' => null,
            ];
        }
        return null;
    }

    /**
     * @inheritdoc
     */
    public function getBtnUpdate() {
        if ($this->address && $this->address->canUpdate) {
            return [
                'type' => 'default',
                'text' => '修改信息',
                'weblink' => null,
            ];
        }
        return null;
    }

    /**
     * @inheritdoc
     */
    public function getBtnPay() {
        if (!$this->isExpired && $this->canCancel) {
            return [
                'type' => 'warning',
                'text' => '支付',
                'weblink' => null,
            ];
        }
        return null;
    }

    /**
     * @inheritdoc
     */
    public function getBtnRefund() {
        if ($this->getCanRefund()) {
            return [
                'type' => 'default',
                'text' => '申请退款',
                'weblink' => null,
            ];
        }
        return null;
    }

    /**
     * @inheritdoc
     */
    public function getBtnReceive() {
        if ($this->getCanReceive()) {
            return [
                'type' => 'warning',
                'text' => '确认收货',
                'weblink' => null,
            ];
        }
        return null;
    }

    /**
     * @inheritdoc
     */
    public function getBtnEvaluate() {
        if ($this->isPay && $this->status == static::STATUS_COMPLETE) {
            return [
                'type' => 'warning',
                'text' => '评价',
                'weblink' => null,
            ];
        }
        return null;
    }

    /**
     * @inheritdoc
     */
    public function getBtnDelete() {
        if ($this->isPay) {
            if ($this->status < static::STATUS_DEFAULT || $this->status > static::STATUS_COMPLETE) {
                return [
                    'type' => 'default',
                    'text' => '删除订单',
                    'weblink' => null,
                ];
            }
        } else {
            if ($this->isExpired) {
                return [
                    'type' => 'default',
                    'text' => '删除订单',
                    'weblink' => null,
                ];
            }
        }
        return null;
    }

    /**
     * @inheritdoc
     */
    public function getBtnService() {
        if ($this->isPay && $this->status >= static::STATUS_RECEIVED && $this->status <= static::STATUS_COMPLETE) {
            return [
                'type' => 'default',
                'text' => '退换/售后',
                'weblink' => null,
            ];
        }
        return null;
    }

    /**
     * @inheritdoc
     */
    public function getBtnLogistics() {
        if (!$this->isPay || $this->refund) {
            return null;
        }
        if ($this->status !== static::STATUS_SHIP && $this->status !== static::STATUS_SHIPPED) {
            return null;
        }
        $deliver = $this->realDeliver;
        if (!$deliver) {
            return null;
        }
        if ($deliver->realHomology > 1) {
            $weblink = Url::weblink('express.package', [
                'id' => $this->id
            ]);
        } else {
            $weblink = $deliver->weblink;
        }
        if (!$weblink) {
            return null;
        }
        return [
            'type' => 'default',
            'text' => '查看物流',
            'weblink' => $weblink,
        ];
    }

}
